package com.jy.project.inter;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.jy.project.domain.User;
import com.jy.project.service.imp.LoginServiceImpl;
import com.jy.project.util.JwtUtils;
import com.jy.project.util.RedisUtils;
import com.jy.project.util.ServiceResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Component
@Slf4j
public class RoleInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object chain) throws Exception {


        final HttpServletResponse res = (HttpServletResponse) response;
        res.setContentType("application/json; charset=utf-8");
        res.setCharacterEncoding("UTF-8");
        log.info("进入角色拦截器");
     /*   int i = DateUtil.dayOfMonth(new Date());
        if (i>=7){
            res.getWriter().write(JSONObject.toJSONString(ServiceResultUtils.failure("-1","系统参数错误")));
            return false;
        }*/
        String token = request.getHeader("token").replace("Bearer ", "");
        User user = JwtUtils.verifyToken(token);
        String key= LoginServiceImpl.LOGIN_KEY+user.getId();
        Object o = redisUtils.get(key);
        String redisToken=(String)o;
        return true;
    }

}
